package com.guoxianwei.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.guoxianwei.scm.domain.Menu;

public class MenuTreeHelper {

	@SuppressWarnings("unchecked")
	public static List<Menu> toTreeMenuList(List<Menu> list) {
		if (list != null) {
			List<Menu> subMenuList = new ArrayList<Menu>();
			List<Menu> parentMenuList = new ArrayList<Menu>();
			for (Menu menu : list) {
				if ("true".equalsIgnoreCase(menu.getIsleaf())) {
					subMenuList.add(menu);
				} else {
					parentMenuList.add(menu);
				}
			}
			if (parentMenuList != null) {
				for (Menu parentMenu : parentMenuList) {
					if (subMenuList != null) {
						for (Menu subMenu : subMenuList) {
							if (parentMenu.getMenuid() == subMenu.getParentid()) {
								parentMenu.addChildMenu(subMenu);
							}
						}
					}
				}
			}
			return parentMenuList;
		}
		return Collections.EMPTY_LIST;
	}
	public static String getMenuTreeJsonString(List<Menu> menulist) {
		
		return GSONUtils.toJson(toTreeMenuList(menulist), false);
	}
}
